﻿using System;

namespace Distribution.General
{
    public static class ConvertMobileHelper
    {
        #region Conversion
        /// <summary>
        /// Converts an object to Int32 or returns Int32.MinValue if it is null or DBNull
        /// </summary>
        public static Int32 ToInt32(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? Int32.Parse(obj.ToString()) : Int32.MinValue;
            }
            catch
            {
                return Int32.MinValue;
            }
        }
        /// <summary>
        /// Converts an object to double or returns double.MinValue if it is null or DBNull
        /// </summary>
        public static double Todouble(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? double.Parse(obj.ToString()) : double.MinValue;
            }
            catch
            {
                return double.MinValue;
            }
        }
        /// <summary>
        /// Converts an object to DateTime or returns DateTime.MinValue if it is null or DBNull
        /// </summary>
        public static DateTime ToDateTime(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? DateTime.Parse(obj.ToString()) : DateTime.MinValue;
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        public static string Tostring(object obj)
        {
            try
            {
                return obj.ToString();

            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Transforms longitude from GPS format to a decimal number.
        /// </summary>
        public static double ConvertLongitudeToDecimal(string Position, string CoordinatedLetter, ref string FormatedView)
        {
            //Created by: GBarzola
            //Creation date: -
            //Last updated by: GBarzola
            //Last update: -

            try
            {
                double SingleValue = 1;
                switch (CoordinatedLetter)
                {
                    case "E":
                        SingleValue = 1;
                        break;
                    case "W":
                        SingleValue = -1;
                        break;
                }
                double NewPosition = 0;
                FormatedView = "0";
                if (Position != "0")
                {
                    string[] arrPosition = Position.Split('.');
                    double previewMinutes = double.Parse(arrPosition[0].Substring(3, 2));
                    double previewDegree = double.Parse(arrPosition[0].Substring(0, 3));

                    double previewSeconds = (double.Parse(arrPosition[1]) / 10000000) * 60;
                    FormatedView = previewDegree + " ° " + previewMinutes + " ' " + previewSeconds + " \" ";
                    // 34º 36' 34.79'' = 34 + ( 36 / 60 ) + ( 34,79 / 3600 ) =  (expresado en decimal)
                    NewPosition = (previewDegree + (previewMinutes / 60) + (previewSeconds / 3600)) * SingleValue;
                }
                return NewPosition;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Transforms latitude from GPS format to a decimal number.
        /// </summary>
        public static double ConvertLatitudeToDecimal(string Position, string CoordinatedLetter, ref string DegreeFormated)
        {
            //Created by: GBarzola
            //Creation date: -
            //Last updated by: GBarzola
            //Last update: -

            try
            {
                double SingleValue = 1;
                switch (CoordinatedLetter)
                {
                    case "N":
                        SingleValue = 1;
                        break;
                    case "S":
                        SingleValue = -1;
                        break;
                }
                double NewPosition = 0;
                DegreeFormated = "0";
                if (Position != "0")
                {
                    string[] arrPosition = Position.Split('.');
                    double previewMinutes = double.Parse(arrPosition[0].Substring(2, 2));
                    double previewDegree = double.Parse(arrPosition[0].Substring(0, 2));
                    double previewSeconds = (double.Parse(arrPosition[1]) / 10000000) * 60;
                    DegreeFormated = previewDegree + " ° " + previewMinutes + " ' " + previewSeconds + " \" ";
                    // 34º 36' 34.79'' = 34 + ( 36 / 60 ) + ( 34,79 / 3600 ) =  (expresado en decimal)
                    NewPosition = (previewDegree + (previewMinutes / 60) + (previewSeconds / 3600)) * SingleValue;
                }
                return NewPosition;
            }
            catch
            {
                return 0;
            }
        }
        #endregion Conversion
    }
}
